﻿using System;
using System.IO;
using Cmpp.Client.Cmpp3Message.Utility;

namespace Cmpp.Client.Cmpp3Message {
    internal class Cmpp3ResponseFactory {
        public static Cmpp3Response GetResponse(byte[] packet) {
            var reader = new CmppMessageReader(packet);
            var hreader = new Cmpp3Header(reader);
            reader.Reset();

            if (hreader.TotalLength != packet.Length) {
                throw new InvalidDataException("packet");
            }

            switch (hreader.Command) {
                case Cmpp3Command.ActiveTestResponse:
                    return new Cmpp3ActiveTestResponse(reader);

                case Cmpp3Command.ConnectResponse:
                    return new Cmpp3ConnectResponse(reader);

                case Cmpp3Command.SubmitResponse:
                    return new Cmpp3SubmitResponse(reader);

                case Cmpp3Command.DeliverRequest:
                    return new Cmpp3DeliverRequest(reader);

                case Cmpp3Command.TerminateResponse:
                    return new Cmpp3TerminateResponse(reader);

                default:
                    throw new NotSupportedException(hreader.Command.ToString());
            }
        }
    }
}